package com.orangelife.mobile.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.curry.android.http.VolleyHttpUtil;
import com.orangelife.mobile.app.application.CurryApplication;

import org.apache.http.util.EncodingUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by wgf on 2016/5/4.
 *
 * @author wgf
 */
public class JsonLocalFileUtil {
    //写数据
    public void writeFile(Activity activity, String fileName, String writestr) throws IOException {
        try {
            FileOutputStream fout = activity.openFileOutput(fileName, Activity.MODE_PRIVATE);
            byte[] bytes = writestr.getBytes();
            fout.write(bytes);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //读数据
    public String readFile(Activity activity, String fileName) throws IOException {
        String res = "";
        try {
            FileInputStream fin = activity.openFileInput(fileName);
            int length = fin.available();
            byte[] buffer = new byte[length];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;

    }

    public static String readFile(Context context, String fileName) {
        return readFile(context, fileName, false);
    }

    public static void ogtest(String fileName, VolleyHttpUtil.ResponseCallBack callBack) {
        ogtest(fileName, callBack, 0);
    }

    public static void ogtest(String fileName, VolleyHttpUtil.ResponseCallBack callBack, int errCode) {
        String json = JsonLocalFileUtil.readFile(CurryApplication.getInstance().getContext(), String.valueOf(fileName) + ".json");
        try {
            callBack.handleResponse(new JSONObject(json), errCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //读数据
    public static String readFile(Context context, String fileName, boolean isDebug) {
        InputStream is;
        BufferedReader reader = null;
        StringBuilder data = new StringBuilder();
        try {
            is = context.getAssets().open(fileName);
            //从给定位置获取文件
//        File file = new File(path);
            reader = new BufferedReader(new InputStreamReader(is));
            //每次读取文件的缓存
            String temp;
            while ((temp = reader.readLine()) != null) {
                data.append(temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭文件流
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (isDebug) {
            Log.e(JsonLocalFileUtil.class.getSimpleName(), data.toString().trim());
        }
        return data.toString().trim();
    }
}
